The LiveView socket for Phoenix Endpoints.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/phoenixframework/phoenix_live_view/llms.txt
Use this file to discover all available pages before exploring further.
Overview
ThePhoenix.LiveView.Socket module provides the Phoenix Socket implementation for LiveView. It can be mounted directly in your endpoint or extended to create custom user sockets with additional channels.
Socket Configuration
Basic Setup
Mount the socket directly in your endpoint:Custom User Socket
To share a transport connection between regular Phoenix channels and LiveView:Runtime Configuration
If you need session options to be set at runtime, use an MFA tuple:Socket Struct
The LiveView socket has the following structure:Fields
The socket identifier
The Phoenix endpoint module
The current LiveView module
The PID of the parent LiveView, if any
The PID of the root LiveView process
The router module
The socket assigns containing application data
Private socket data not sent to the client
Redirect information if the socket has been redirected
The host URI or indicator that view is not mounted at router
The PID of the transport process
Whether the socket is sticky (used for stateful components)
Channels
The socket automatically defines two channel routes:"lv:*"- Phoenix.LiveView.Channel (LiveView connections)"lvu:*"- Phoenix.LiveView.UploadChannel (File upload connections)
Callbacks
connect/3
Called when the socket connects.Connection parameters from the client
The socket struct
Additional connection information (session, peer data, etc.)
Returns
{:ok, socket} to accept the connection or :error to reject itid/1
Returns the socket identifier for the connection.The socket struct
The socket ID used for presence tracking and socket management
live_socket_id from session:
Using the Socket Module
When youuse Phoenix.LiveView.Socket, the following are automatically added:
use Phoenix.Socket- Base Phoenix Socket behavior- Channel definitions for LiveView and Upload channels
- Default
connect/3callback (can be overridden) - Default
id/1callback (can be overridden)